CHICKEN Scheme API

Module (chicken flonum)

Because CHICKEN supports a full numeric tower, operations can sometimes incur a subtantial overhead to simply detect the type of numbers you're passing in. When you know you're definitely dealing only with flonums, you can choose to use flonum-specific operations to avoid this overhead.

This is purely a performance hack. You might want to consider adding type annotations instead, this often gives the same performance boost without having to rewrite all numeric operators in your code.

Arithmetic floating-point operations

(fp+ X Y)(fp- X Y)(fp* X Y)(fp/ X Y)(fp*+ X Y Z)(fpgcd X Y)(fpneg X)(fpmin X Y)(fpmax X Y)(fp= X Y)(fp> X Y)(fp< X Y)(fp>= X Y)(fp<= X Y)(fpfloor X)(fpceiling X)(fptruncate X)(fpround X)(fpsin X)(fpcos X)(fptan X)(fpasin X)(fpacos X)(fpatan X)(fpatan2 X Y)(fpsinh X)(fpcosh X)(fptanh X)(fpasinh X)(fpacosh X)(fpatanh X)(fplog X)(fpexp X)(fpexpt X Y)(fpsqrt X)(fpabs X)(fpinteger? X)

Arithmetic floating-point operations.

In safe mode, these procedures throw a type error when given non-float arguments. In unsafe mode, these procedures do not check their arguments. A non-flonum argument in unsafe mode can crash the application. fp*+ implements fused multiply-add (X * Y) + Z.

Note: fpround uses the rounding mode that your C library implements, which is usually different from R7RS.

Flonum limits

maximum-flonumminimum-flonumflonum-radixflonum-epsilonflonum-precisionflonum-decimal-precisionflonum-maximum-exponentflonum-minimum-exponentflonum-maximum-decimal-exponentflonum-minimum-decimal-exponent

Platform-specific flonum limits.

[procedure] (flonum-print-precision [PRECISION])

Gets and sets the number of significant digits printed for a floating-point number. PRECISION must be a positive fixnum. Returns the setting that was previously in effect.

The default print precision is 15 on nearly all systems, and 7 on the rare system on which the double type is only single-precision.

Note: To ensure read/write invariance for all floating-point numbers, you must increase print precision from 15 to 17 (or from 7 to 9). For example:

> (define a (expt 2 -53))
> (define b (+ a (* 2 (expt 10 -32))))
> (eqv? a b)
#f
> (flonum-print-precision 15)
> (cons a b)
(1.11022302462516e-16 .
 1.11022302462516e-16)            ;; same printed representation
> (flonum-print-precision 17)
> (cons a b)
(1.1102230246251565e-16 .
 1.1102230246251568e-16)          ;; differs in last place

On the downside, this will result in unnecessarily precise representations of many numbers:

> (flonum-print-precision 17)
> 0.1
0.10000000000000001

The maximum number of decimal digits required to uniquely represent all floating-point numbers of a certain precision is given by the formula ceil(1+N*log10(2)), where N is the number of bits of precision; for double-precision, N=53.


Previous: Module (chicken fixnum)

Next: Module (chicken foreign)